package com.caocaog.market.toccpt.jedis;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.Bean;
import redis.clients.jedis.*;

import java.util.LinkedList;
import java.util.List;

/**
 * @author liguang
 * @version 1.0.0
 * @Project jeecg-boot
 * @Package com.caocaog.market.toccpt.jedis
 * @ClassName CcptJedisConfig.java
 * @createTime 2021年12月27日 14:41:00
 * @Description jedis配置
 */
@Slf4j
public class CcptJedisConfig {
    private Jedis jedis ;
    public Jedis getJedis() {
        // 如果连接redis成功,直接返回Jedis实例;如果连接不成功,就再次连接;
        if(null==jedis || !jedis.ping().equalsIgnoreCase("PONG")){
            log.info("redis服务器连接失败；重新获取连接,重新连接,直到连接成功为止");
            return connectJedis();
        }else{
            return jedis;
        }
    }
    public Jedis connectJedis() {
         //创建Jedis实例，连接本地Redis服务
        try{
            String host = "r-2zekrimcnlq5fc55p0pd.redis.rds.aliyuncs.com";
            jedis = new Jedis(host, 6379);
            jedis.auth("Ccpt2020");
            log.info("Redis服务器连接成功！");
            //查看服务是否正在运行
            log.info("服务正在运行：" + jedis.ping());
        }catch(Exception e){
            log.info("远程阿里云redis失败!");
            e.printStackTrace();
        }
        return jedis;
    }

//    public static void main(String[] args) {
//       new CcptJedisConfig().connectJedis();
//    }



}
